package com.okyai.kt.service.impl;

import java.util.List;
import com.okyai.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.okyai.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.okyai.kt.domain.TopicPlan;
import com.okyai.kt.mapper.TopicMapper;
import com.okyai.kt.mapper.TopicPlanMapper;
import com.okyai.kt.domain.Topic;
import com.okyai.kt.service.ITopicService;
import com.okyai.common.core.text.Convert;

/**
 * 课题Service业务层处理
 * 
 * @author okyai
 * @date 2021-03-20
 */
@Service
public class TopicServiceImpl implements ITopicService 
{
    @Autowired
    private TopicMapper topicMapper;
    
    @Autowired
    private TopicPlanMapper topicPlanMapper;

    /**
     * 查询课题
     * 
     * @param topicId 课题ID
     * @return 课题
     */
    @Override
    public Topic selectTopicById(Long topicId)
    {
        return topicMapper.selectTopicById(topicId);
    }

    /**
     * 查询课题列表
     * 
     * @param topic 课题
     * @return 课题
     */
    @Override
    public List<Topic> selectTopicList(Topic topic)
    {
        return topicMapper.selectTopicList(topic);
    }

    /**
     * 新增课题
     * 
     * @param topic 课题
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTopic(Topic topic)
    {
        topic.setCreateTime(DateUtils.getNowDate());
        int rows = topicMapper.insertTopic(topic);
        insertTopicPlan(topic);
        return rows;
    }

    /**
     * 修改课题
     * 
     * @param topic 课题
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTopic(Topic topic)
    {
        topic.setUpdateTime(DateUtils.getNowDate());
        //topicMapper.deleteTopicPlanByTopicId(topic.getTopicId());
        //insertTopicPlan(topic);
        
        return topicMapper.updateTopic(topic);
    }

    /**
     * 删除课题对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTopicByIds(String ids)
    {
        topicMapper.deleteTopicPlanByTopicIds(Convert.toStrArray(ids));
        return topicMapper.deleteTopicByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课题信息
     * 
     * @param topicId 课题ID
     * @return 结果
     */
    @Override
    public int deleteTopicById(Long topicId)
    {
        topicMapper.deleteTopicPlanByTopicId(topicId);
        return topicMapper.deleteTopicById(topicId);
    }

    /**
     * 新增项目方案组信息
     * 
     * @param topic 课题对象
     */
    public void insertTopicPlan(Topic topic)
    {
        List<TopicPlan> topicPlanList = topic.getTopicPlanList();
        Long topicId = topic.getTopicId();
        if (StringUtils.isNotNull(topicPlanList))
        {
            List<TopicPlan> list = new ArrayList<TopicPlan>();
            for (TopicPlan topicPlan : topicPlanList)
            {
                topicPlan.setTopicId(topicId);
                list.add(topicPlan);
            }
            if (list.size() > 0)
            {
                topicMapper.batchTopicPlan(list);
            }
        }
    }
    
    
    public void saveTopicPlan(Topic topic)
    {
        List<TopicPlan> topicPlanList = topic.getTopicPlanList();
        Long topicId = topic.getTopicId();
        if (StringUtils.isNotNull(topicPlanList))
        {
            List<TopicPlan> list = new ArrayList<TopicPlan>();
            for (TopicPlan topicPlan : topicPlanList)
            {
                topicPlan.setTopicId(topicId);
                if(topicPlan.getPlanId()==null) {
                	list.add(topicPlan);
                }else {
                	topicPlanMapper.updateTopicPlan(topicPlan);
                }
            }
            if (list.size() > 0)
            {
                topicMapper.batchTopicPlan(list);
            }
        }
    }
    

}
